﻿
using NikonWrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CameriaControl
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        NikonDevice _device;
        NikonManager manager;
        public MainWindow()
        {
            InitializeComponent();

            manager = new NikonManager("Type0014.md3");//14 11

            manager.DeviceAdded += manager_DeviceAdded;
            manager.DeviceRemoved += Manager_DeviceRemoved; ;

            //  SetVals();
        }

        private void Manager_DeviceRemoved(NikonManager sender, NikonDevice device)
        {
            this._device = null;
          
        }

        void SetVals()
        {
            tuXiangChicun.SelectedIndex = 1;
        }

        void getParameterAndSet()
        {
            try
            {
                var exposureModeEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ExposureMode);
                var exposureModeList = argsVal(exposureModeEnum);
                baoGuangMoshi.ItemsSource = exposureModeList;
                var index1 = exposureModeEnum.Index;
                baoGuangMoshi.SelectedIndex = index1;


                //shutter speed
                NikonEnum shutterSpeedEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed);
                var shutterList = argsVal(shutterSpeedEnum);
                baoGuangShijian.ItemsSource = shutterList;
                var index2 = shutterSpeedEnum.Index;              
                baoGuangShijian.SelectedIndex = index2;


                NikonEnum SensitivityEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity);
                var SensitivityEnumList = argsVal(SensitivityEnum);
                isoGanguangdu.ItemsSource = SensitivityEnumList;
                var index3 = SensitivityEnum.Index;
                isoGanguangdu.SelectedIndex = index3;

                var CapabilityEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ImageSize);
                var capabilityList = argsVal(CapabilityEnum);
                tuXiangChicun.ItemsSource = capabilityList;
                var index4 = CapabilityEnum.Index;
                tuXiangChicun.SelectedIndex = index4;

                var WBModeEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_WBMode);
                var wbModeList = argsVal(WBModeEnum);
                BaipingHengMoshi.ItemsSource = wbModeList;
                var index5 = WBModeEnum.Index;
                BaipingHengMoshi.SelectedIndex = index5;



                NikonEnum apertureEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_Aperture);
                var apertureList = argsVal(apertureEnum);
                guangQuan.ItemsSource = apertureList;
                var index7 = apertureEnum.Index;
                guangQuan.SelectedIndex = index7;


                //NikonEnum saveMediaEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_SaveMedia);
                //var saveMediaList = argsVal(saveMediaEnum);
                //cuntumoshi.ItemsSource = saveMediaList;
                //var index8 = saveMediaEnum.Index;
                //cuntumoshi.SelectedIndex = index8;
            }
            catch (Exception e)
            {

               
            }
            
        }

        private List<string> argsVal(NikonEnum argsEnum)
        {
            List<string> args = new List<string>();
            for (int i = 0; i < argsEnum.Length; i++)
            {
                args.Add(argsEnum[i].ToString());
            }
            return args;
        }

        void manager_DeviceAdded(NikonManager sender, NikonDevice device)
        {
            if (_device == null)
            {
                // Save device
                _device = device;

                // _device.LiveViewEnabled = true; //实时图像
                //

                getParameterAndSet();
            }
        }

        private void baoGuangShijian_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var shijianBao = sender as ComboBox;
                if (shijianBao != null)
                {
                    var index = shijianBao.SelectedIndex;
                    NikonEnum shutterSpeedEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed);
                    shutterSpeedEnum.Index = index;
                    _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_ShutterSpeed, shutterSpeedEnum);
                }
            }
            catch (Exception)
            {

                
            }
      
        }

        private void isoGanguangdu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var ganguandu = sender as ComboBox;
            if (ganguandu != null)
            {
                var index = ganguandu.SelectedIndex;
                NikonEnum ganguanduEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity);
                ganguanduEnum.Index = index; 
                _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_Sensitivity, ganguanduEnum);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            manager?.Shutdown();
            if (_device != null)
            {
                _device=null;
            }

        }

        private void cuntumoshi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //var cuntumoshi = sender as ComboBox;
            //if (cuntumoshi != null)
            //{
            //    var index = cuntumoshi.SelectedIndex;
            //    NikonEnum saveSdEnum = _device.GetEnum(eNkMAIDCapability.kNkMAIDCapability_SaveMedia);
            //    saveSdEnum.Index = index;
            //    _device.SetEnum(eNkMAIDCapability.kNkMAIDCapability_SaveMedia, saveSdEnum);
            //}
        }
    }
}
